﻿using System;
using PmSadis.WebFormsUi.ExpandedControls;
using DevExpress.Web.Data;
using DevExpress.Web.ASPxGridView;
using PmSadis.Repositories.Abstract;
using System.Linq;
using System.Collections.Generic;
using PmSadis.CsExtantions;
using DevExpress.Web.ASPxGridLookup;

namespace PmSadis.WebFormsUi.Operator
{
    public partial class Objects : System.Web.UI.Page
    {
        private const string __operExpandedObjectId = "oper-expanded-object-id";
        private const string __operExpandedObjectType = "oper-expanded-object-type";

        private IListRepository<Models.ContractObject> _repo = new Repositories.ObjectRepository();
        private IListRepository<Models.ObjectElement> _repoObjectElements = new Repositories.ObjectElementRepository();
        private IListRepository<Models.Element> _repoElements = new Repositories.ElementRepository();
        private IListRepository<Models.Unit> _repoUnits = new Repositories.UnitRepository();

        protected void gvObjects_Init(object sender, EventArgs e)
        {
            gvObjects.SetDefaultSettings();
        }

        public IQueryable<Models.ContractObject> GetAll()
        {
            return _repo.All.OrderBy(x=>x.Code);
        }
        public void InsertObject(string name, string code, Models.Enums.ObjectType objectType)
        {
            var @object = new PmSadis.Models.ContractObject {
                ObjectId=0,
                Name=name,
                Code=code,
                ObjectType=objectType
            };
            _repo.Edit(@object);
        }
        public void UpdateObject(int objectId, DateTime dateCreate, string name, string code, Models.Enums.ObjectType objectType)
        {
            var @object = new PmSadis.Models.ContractObject
            {
                ObjectId = objectId,
                DateCreate=dateCreate,
                Name = name,
                Code = code,
                ObjectType = objectType
            };
            _repo.Edit(@object);
        }
        public void DeleteObject(int objectId)
        {
            _repo.Delete(objectId);
        }

        protected void gvObjects_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
        {
            var grid = sender as ASPxGridView;
            var dateCreate = grid.GetRowValues(grid.EditingRowVisibleIndex, "DateCreate");
            e.NewValues["DateCreate"] = dateCreate;
        }
        

        public IQueryable<Models.ObjectElement> ObjectElements(int objectId)
        {
            var results = _repoObjectElements.All.Where(x => x.ObjectId == objectId);
            return results;
        }
        public void UpdateObjectElement(int objectElementId, DateTime dateCreate, int elementId, int objectId, double begKm, double? endKm, string location, double value)
        {
            var objectElement = new PmSadis.Models.ObjectElement {
                ObjectElementId=objectElementId,
                DateCreate=dateCreate,
                ElementId=elementId,
                ObjectId=objectId,
                BegKm=begKm,
                EndKm=endKm,
                Location=location,
                Value=value
            };
            _repoObjectElements.Edit(objectElement);
        }
        public void InsertObjectElement(int elementId, int objectId, double begKm, double? endKm, string location, double value)
        {
            var objectElement = new PmSadis.Models.ObjectElement
            {
                ObjectElementId = 0,
                ElementId = elementId,
                ObjectId = objectId,
                BegKm = begKm,
                EndKm = endKm,
                Location = location,
                Value = value
            };
            _repoObjectElements.Edit(objectElement);
        }
        public void DeleteObjectElement(int objectElementId)
        {
            _repoObjectElements.Delete(objectElementId);
        }

        protected void gvObjectElements_Init(object sender, EventArgs e)
        {
            var grid = sender as ASPxGridView;
            grid.ForceDataRowType(typeof(Models.ObjectElement));
        }
        protected void gvObjectElements_RowUpdating(object sender, ASPxDataUpdatingEventArgs e)
        {
            var grid = sender as ASPxGridView;
            var lookup = grid.FindEditRowCellTemplateControl(grid.Columns["ElementID"] as GridViewDataColumn, "lucElements") as ASPxGridLookup;

            var dateCreate = grid.GetRowValues(grid.EditingRowVisibleIndex, "DateCreate");

            e.NewValues["DateCreate"] = dateCreate;
            e.NewValues["ElementId"] = lookup.Value;
            e.NewValues["ObjectId"] = grid.GetMasterRowKeyValue();
        }
        protected void gvObjectElements_RowInserting(object sender, ASPxDataInsertingEventArgs e)
        {
            var grid = sender as ASPxGridView;
            var lookup = grid.FindEditRowCellTemplateControl(grid.Columns["ElementID"] as GridViewDataColumn, "lucElements") as ASPxGridLookup;

            e.NewValues["ElementId"] = lookup.Value;
            e.NewValues["ObjectId"] = grid.GetMasterRowKeyValue();
        }
        protected void gvObjectElements_BeforePerformDataSelect(object sender, EventArgs e)
        {
            var grid = sender as ASPxGridView;

            var objectId = grid.GetMasterRowKeyValue();
            var objectType = gvObjects.GetRowValuesByKeyValue(objectId, "ObjectType");
            Session[__operExpandedObjectId] = objectId;
            Session[__operExpandedObjectType] = objectType;
            grid.SetDefaultSettings();
        }
        

        public IQueryable Elements(int objectType)
        {
            var type = (Models.Enums.ObjectType)objectType;
            var results = _repoElements.All.Where(x => x.ObjectType == type).Select(x => new
            {
                ElementId=x.ElementId,
                UnitName=x.Unit.Name,
                Name=x.Name,
                ElementType=x.ElementType
            });
            return results.OrderBy(x=>x.Name);
        }

        protected void lucElements_Init(object sender, EventArgs e)
        {
            var lookup = sender as ASPxGridLookup;
            var container = lookup.NamingContainer as GridViewDataItemTemplateContainer;
            if (!container.Grid.IsNewRowEditing)
            {
                var elementId = (int)container.Grid.GetRowValues(container.VisibleIndex, container.Column.FieldName);
                lookup.GridView.Selection.SelectRowByKey(elementId);
                lookup.ClientEnabled = false;
            }
            else
            {

            }
        }

        public IQueryable<PmSadis.Models.Unit> Units()
        {
            return _repoUnits.All;
        }
    }
}